package com.eblacorp.archive.security;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.eblacorp.archive.security.ldap.dao.ContactDao;
import com.eblacorp.archive.security.ldap.model.Contact;
import com.eblacorp.archive.security.ldap.model.Group;
import com.eblacorp.archive.security.ldap.model.SearchCriteria;
import com.eblacorp.archive.security.ldap.service.ContactService;
import com.eblacorp.archive.security.ldap.service.GroupService;
import com.eblacorp.archive.service.security.UserAccessService;

/**
 * @author Mahmoud.Ezzat
 * 
 */
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

	private UserAccessService userAccessService;
	private ContactService contactService;
	private GroupService groupService;

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {

		Set<String> roles = AuthorityUtils.authorityListToSet(authentication
				.getAuthorities());
		if (roles == null) {
			throw new AuthenticationServiceException(
					"This User Is Not Authorized");
		}

		if (!userAccessService.isUserAllowToAccess(authentication.getName())) {
			throw new AuthenticationServiceException(
					"This User Is Not Authonticated");
		}

		UserDetails user = (UserDetails) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();

		Contact c = contactService.findContactBySAMName(user.getUsername());
		//List<Contact> cList = contactService.searchContact("admin");
		List<Contact> cList = contactService.listContacts();
		List<Group> gList = groupService.findAll();
		List<Group> gList2 = groupService.find(new SearchCriteria(
				"ARCH_ADMIN"));
		//contactService.addContactToUOGroup(c, gList2.get(0));
		//contactService.removeContactToUOGroup(c, gList2.get(0));
		List<Group> gList3 = groupService.findAll();
		response.sendRedirect(request.getContextPath() + "/pages/home.xhtml");

	}

	public UserAccessService getUserAccessService() {
		return userAccessService;
	}

	public void setUserAccessService(UserAccessService userAccessService) {
		this.userAccessService = userAccessService;
	}

	public ContactService getContactService() {
		return contactService;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

}
